package cn.s1995.sloth.app.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.constant.BizCst;
import cn.s1995.sloth.bizs.entity.*;
import cn.s1995.sloth.bizs.oo.req.OrderListREQ;
import cn.s1995.sloth.bizs.oo.vo.*;
import cn.s1995.sloth.bizs.service.*;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * CommonController
 *
 * @author Lex
 * @description 我的
 * @date 2023/12/23 17:14
 */
@RestController
@RequestMapping("/m/my")
@RequiredArgsConstructor
@Tag(description = "MyController", name = "APP-我的")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ApiMyController {

    private final OrderService orderService;
    private final UserLikeService userLikeService;
    private final UserCollectionService userCollectionService;
    private final UserFollowService userFollowService;
    private final UserFeedbackService userFeedbackService;


    // @ApiOperationSupport(order = -1)
    // @Operation(summary = "注销账号", description = "注销账号")
    // @SysLog("注销账号")
    // @DeleteMapping("/account")
    // public R logoutAccount() {
    //     slothUser user = SecurityUtils.getUser();
    //
    //     return R.ok();
    // }

    @ApiOperationSupport(order = 1)
    @Operation(summary = "我的订单", description = "我的订单")
    @GetMapping("/order/page")
    public R<IPage<OrderVO>> getPageMyOrder(@ParameterObject Page page, @ParameterObject OrderListREQ req) {
    	if (ObjectUtil.isNotNull(req.getState()) && req.getState() == 7) {
    		req.setUid(SecurityUtils.getUser().getId());
    		return R.ok(orderService.getHasRefundOrderPage(page, req));
    	} else {
            MPJLambdaWrapper<Order> wrapper = new MPJLambdaWrapper<Order>()
                    .selectAll(Order.class)
                    .eq(Order::getUid, SecurityUtils.getUser().getId())
                    .eq(ObjectUtil.isNotNull(req.getState()), Order::getState, req.getState())
                    .orderByDesc(Order::getCreateTime);
            if(StrUtil.isNotBlank(req.getSearch())){
                wrapper.and(
                        StrUtil.isNotEmpty(req.getSearch()),
                        w -> w.like(Order::getProdName, req.getSearch())
                                .or()
                                .like(Order::getId, req.getSearch())
                                .or()
                                .like(Order::getUserNickname, req.getSearch())
                                .or()
                                .like(Order::getShopName, req.getSearch())
                );
            }
            return R.ok(orderService.pageWithOrderItem(page, wrapper));
    	}
    }

    // @ApiOperationSupport(order = 2)
    // @Operation(summary = "我的评价", description = "我的评价")
    // @GetMapping("/comment")
    // public R getMyProdComment() {
    //     slothUser user = SecurityUtils.getUser();
    //
    //     return R.ok();
    // }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "我的收藏-产品", description = "我的收藏-产品")
    @GetMapping("/collection/prod/page")
    public R<IPage<MyCollectionProdVO>> getPageMyCollectionProd(@ParameterObject Page page, @ParameterObject UserCollection req) {
        MPJLambdaWrapper<UserCollection> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Prod.class)
                .leftJoin(Prod.class, Prod::getId, UserCollection::getBindId)
                .eq(UserCollection::getUid, SecurityUtils.getUser().getId())
                .eq(UserCollection::getType, BizCst.UserCollection.Type.prod);

        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(Prod::getName, req.getSearch())
                        .or()
                        .like(Prod::getBrief, req.getSearch())
        );

        return R.ok(userCollectionService.selectJoinListPage(page, MyCollectionProdVO.class, wrapper));
    }

    @ApiOperationSupport(order = 21)
    @Operation(summary = "我的收藏-视频", description = "我的收藏-视频")
    @GetMapping("/collection/video/page")
    public R<IPage<MyFollowVideoVO>> getPageMyCollectionVideo(@ParameterObject Page page, @ParameterObject UserCollection req) {
        MPJLambdaWrapper<UserCollection> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Video.class)
                .leftJoin(Video.class, Video::getId, UserCollection::getBindId)
                .eq(UserCollection::getUid, SecurityUtils.getUser().getId())
                .eq(UserCollection::getType, BizCst.UserCollection.Type.video);

        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(Video::getDesc, req.getSearch())
        );

        return R.ok(userCollectionService.selectJoinListPage(page, MyFollowVideoVO.class, wrapper));
    }

    @ApiOperationSupport(order = 30)
    @Operation(summary = "我的关注-店铺", description = "我的关注-店铺")
    @GetMapping("/followed/shop/page")
    public R<IPage<MyFollowShopVO>> getPageMyFollowedShop(@ParameterObject Page page, @ParameterObject UserFollow req) {
        MPJLambdaWrapper<UserFollow> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Shop.class)
                .leftJoin(Shop.class, Shop::getId, UserFollow::getBindId)
                .eq(UserFollow::getUid, SecurityUtils.getUser().getId())
                .eq(UserFollow::getType, BizCst.UserFollow.Type.shop);

        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(Shop::getName, req.getSearch())
        );

        return R.ok(userFollowService.selectJoinListPage(page, MyFollowShopVO.class, wrapper));
    }

    @ApiOperationSupport(order = 40)
    @Operation(summary = "我的点赞-视频", description = "我的点赞-视频")
    @GetMapping("/like/video/page")
    public R<IPage<MyFollowVideoVO>> getPageMyLikeVideo(@ParameterObject Page page, @ParameterObject UserLike req) {
        MPJLambdaWrapper<UserLike> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Video.class)
                .leftJoin(Video.class, Video::getId, UserLike::getBindId)
                .eq(UserLike::getUid, SecurityUtils.getUser().getId())
                .eq(UserLike::getType, BizCst.UserLike.Type.video);

        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(Video::getDesc, req.getSearch())
        );

        return R.ok(userLikeService.selectJoinListPage(page, MyFollowVideoVO.class, wrapper));
    }

    @ApiOperationSupport(order = 50)
    @Operation(summary = "我的意见反馈", description = "我的意见反馈")
    @GetMapping("/feedback/page")
    public R<IPage<UserFeedback>> getPageMyFeedback(@ParameterObject Page page, @ParameterObject UserFeedback req) {
        LambdaQueryWrapper<UserFeedback> wrapper = Wrappers.lambdaQuery(UserFeedback.class)
                .eq(UserFeedback::getUid, SecurityUtils.getUser().getId());
        wrapper.eq(Objects.nonNull(req.getType()), UserFeedback::getType, req.getType());
        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(UserFeedback::getTitle, req.getSearch())
                        .or()
                        .like(UserFeedback::getContent, req.getSearch())
                        .or()
                        .like(UserFeedback::getEmail, req.getSearch())
        );
        return R.ok(userFeedbackService.page(page, wrapper));
    }

    @ApiOperationSupport(order = 50)
    @Operation(summary = "我的意见反馈-添加", description = "我的意见反馈-添加")
    @PostMapping("/feedback")
    public R<Boolean> saveMyFeedback(@ParameterObject UserFeedback req) {
        req.setUid(SecurityUtils.getUser().getId());
        return R.ok(userFeedbackService.save(req));
    }


}
